# -*- coding: utf-8 -*-
import arcpy

arcpy.env.workspace = r"d:\Users\LEON\Desktop\哈拉\150524库伦旗0\150524库伦旗.mdb"
arcpy.env.overwriteOutput = True
XZDW_clip = "XZDW_clip"
DLTB_Clip = "DLTB_Clip"
DLTB_Clip__2_ = DLTB_Clip
DLTB_Clip__3_ = DLTB_Clip__2_
XZDW_clip_SpatialJoin = "XZDW_clip_SpatialJoin"

# Process: 添加字段
# arcpy.DeleteField_management(DLTB_Clip,"DisNO")
XZDW_LIST = arcpy.ListFields(XZDW_clip)
XZDW_FIELD = []
for each in XZDW_LIST:
    XZDW_FIELD.append(each.name)
# DLTB_LIST = arcpy.ListFields(DLTB_Clip)
if "KD" in XZDW_FIELD and "KCBL" in XZDW_FIELD:
    arcpy.AddField_management(DLTB_Clip, "KCMJ", "DOUBLE", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")
    arcpy.AddField_management(DLTB_Clip, "KCXZDWMJ", "DOUBLE", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")
    arcpy.AddField_management(XZDW_clip, "长度", "DOUBLE", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")
    # Process: 计算字段
    arcpy.CalculateField_management(XZDW_clip,"长度","!shape.length!","PYTHON_9.3")
    # arcpy.CalculateField_management(DLTB_Clip, "DisNO", "bh()", "PYTHON_9.3", "count = 0\\ndef bh():\\n    global count\\n    count +=1\\n    return \"X\"+str(count)\\n")

    # Process: 空间连接
    # arcpy.SpatialJoin_analysis(XZDW_clip, DLTB_Clip, XZDW_clip_SpatialJoin, "JOIN_ONE_TO_ONE", "KEEP_ALL", "BSM \"BSM\" true true false 4 Long 0 0 ,First,#,XZDW_clip,BSM,-1,-1;YSDM \"YSDM\" true true false 10 Text 0 0 ,First,#,XZDW_clip,YSDM,-1,-1;DLBM \"DLBM\" true true false 4 Text 0 0 ,First,#,XZDW_clip,DLBM,-1,-1;DLMC \"DLMC\" true true false 60 Text 0 0 ,First,#,XZDW_clip,DLMC,-1,-1;XZDWYBH \"XZDWYBH\" true true false 8 Text 0 0 ,First,#,XZDW_clip,XZDWYBH,-1,-1;XZDWBH \"XZDWBH\" true true false 8 Text 0 0 ,First,#,XZDW_clip,XZDWBH,-1,-1;CD \"CD\" true true false 8 Double 0 0 ,First,#,XZDW_clip,CD,-1,-1;KD \"KD\" true true false 8 Double 0 0 ,First,#,XZDW_clip,KD,-1,-1;XZDWMJ \"XZDWMJ\" true true false 8 Double 0 0 ,First,#,XZDW_clip,XZDWMJ,-1,-1;XZDWMC \"XZDWMC\" true true false 60 Text 0 0 ,First,#,XZDW_clip,XZDWMC,-1,-1;QSDWDM1 \"QSDWDM1\" true true false 19 Text 0 0 ,First,#,XZDW_clip,QSDWDM1,-1,-1;QSDWMC1 \"QSDWMC1\" true true false 60 Text 0 0 ,First,#,XZDW_clip,QSDWMC1,-1,-1;QSDWDM2 \"QSDWDM2\" true true false 19 Text 0 0 ,First,#,XZDW_clip,QSDWDM2,-1,-1;QSDWMC2 \"QSDWMC2\" true true false 60 Text 0 0 ,First,#,XZDW_clip,QSDWMC2,-1,-1;KCTBBH1 \"KCTBBH1\" true true false 8 Text 0 0 ,First,#,XZDW_clip,KCTBBH1,-1,-1;KCTBDWDM1 \"KCTBDWDM1\" true true false 19 Text 0 0 ,First,#,XZDW_clip,KCTBDWDM1,-1,-1;KCTBBH2 \"KCTBBH2\" true true false 8 Text 0 0 ,First,#,XZDW_clip,KCTBBH2,-1,-1;KCTBDWDM2 \"KCTBDWDM2\" true true false 19 Text 0 0 ,First,#,XZDW_clip,KCTBDWDM2,-1,-1;QSXZ \"QSXZ\" true true false 2 Text 0 0 ,First,#,XZDW_clip,QSXZ,-1,-1;KCBL \"KCBL\" true true false 8 Double 0 0 ,First,#,XZDW_clip,KCBL,-1,-1;BGJLH \"BGJLH\" true true false 20 Text 0 0 ,First,#,XZDW_clip,BGJLH,-1,-1;BGRQ \"BGRQ\" true true false 8 Date 0 0 ,First,#,XZDW_clip,BGRQ,-1,-1;DLBZ \"地类备注\" true true false 10 Text 0 0 ,First,#,XZDW_clip,DLBZ,-1,-1;SHAPE_Length \"SHAPE_Length\" false true true 8 Double 0 0 ,First,#,XZDW_clip,SHAPE_Length,-1,-1;BSM_1 \"BSM_1\" true true false 4 Long 0 0 ,First,#,DLTB_Clip,BSM,-1,-1;YSDM_1 \"YSDM_1\" true true false 10 Text 0 0 ,First,#,DLTB_Clip,YSDM,-1,-1;TBYBH \"TBYBH\" true true false 8 Text 0 0 ,First,#,DLTB_Clip,TBYBH,-1,-1;TBBH \"TBBH\" true true false 8 Text 0 0 ,First,#,DLTB_Clip,TBBH,-1,-1;DLBM_1 \"DLBM_1\" true true false 4 Text 0 0 ,First,#,DLTB_Clip,DLBM,-1,-1;DLMC_1 \"DLMC_1\" true true false 60 Text 0 0 ,First,#,DLTB_Clip,DLMC,-1,-1;长度 \"长度\" true true false 3 Text 0 0 ,First,#,XZDW_Clip,长度,-1,-1;QSDWDM \"QSDWDM\" true true false 19 Text 0 0 ,First,#,DLTB_Clip,QSDWDM,-1,-1;QSDWMC \"QSDWMC\" true true false 60 Text 0 0 ,First,#,DLTB_Clip,QSDWMC,-1,-1;ZLDWDM \"ZLDWDM\" true true false 19 Text 0 0 ,First,#,DLTB_Clip,ZLDWDM,-1,-1;ZLDWMC \"ZLDWMC\" true true false 60 Text 0 0 ,First,#,DLTB_Clip,ZLDWMC,-1,-1;GDLX \"GDLX\" true true false 2 Text 0 0 ,First,#,DLTB_Clip,GDLX,-1,-1;KCLX \"KCLX\" true true false 2 Text 0 0 ,First,#,DLTB_Clip,KCLX,-1,-1;KCDLBM \"KCDLBM\" true true false 4 Text 0 0 ,First,#,DLTB_Clip,KCDLBM,-1,-1;TKXS \"TKXS\" true true false 8 Double 0 0 ,First,#,DLTB_Clip,TKXS,-1,-1;TBMJ \"TBMJ\" true true false 8 Double 0 0 ,First,#,DLTB_Clip,TBMJ,-1,-1;XZDWMJ_1 \"XZDWMJ_1\" true true false 8 Double 0 0 ,First,#,DLTB_Clip,XZDWMJ,-1,-1;LXDWMJ \"LXDWMJ\" true true false 8 Double 0 0 ,First,#,DLTB_Clip,LXDWMJ,-1,-1;TKMJ \"TKMJ\" true true false 8 Double 0 0 ,First,#,DLTB_Clip,TKMJ,-1,-1;TBDLMJ \"TBDLMJ\" true true false 8 Double 0 0 ,First,#,DLTB_Clip,TBDLMJ,-1,-1;PZWH \"PZWH\" true true false 255 Text 0 0 ,First,#,DLTB_Clip,PZWH,-1,-1;BGJLH_1 \"BGJLH_1\" true true false 20 Text 0 0 ,First,#,DLTB_Clip,BGJLH,-1,-1;BGRQ_1 \"BGRQ_1\" true true false 8 Date 0 0 ,First,#,DLTB_Clip,BGRQ,-1,-1;DLBZ_1 \"DLBZ_1\" true true false 10 Text 0 0 ,First,#,DLTB_Clip,DLBZ,-1,-1;GDPDJ \"GDPDJ\" true true false 2 Text 0 0 ,First,#,DLTB_Clip,GDPDJ,-1,-1;XZGDJFLY \"新增耕地经费来源\" true true false 50 Text 0 0 ,First,#,DLTB_Clip,XZGDJFLY,-1,-1;SHAPE_Length_1 \"SHAPE_Length\" false true true 8 Double 0 0 ,First,#,DLTB_Clip,SHAPE_Length,-1,-1;SHAPE_Area \"SHAPE_Area\" false true true 8 Double 0 0 ,First,#,DLTB_Clip,SHAPE_Area,-1,-1;DisNO \"DisNO\" true true false 0 Text 0 0 ,Join,\",\",DLTB_Clip,DisNO,-1,-1", "WITHIN", "", "")

    # arcpy.SpatialJoin_analysis(XZDW_clip, DLTB_Clip, XZDW_clip_SpatialJoin, "JOIN_ONE_TO_ONE", "KEEP_ALL", "KD \"KD\" true true false 8 Double 0 0 ,First,#,XZDW_clip,KD,-1,-1;KCBL \"KCBL\" true true false 8 Double 0 0 ,First,#,XZDW_clip,KCBL,-1,-1;长度 \"长度\" true true false 8 Double 0 0 ,First,#,XZDW_clip,长度,-1,-1;DisNO \"DisNO\" true true false 255 Text 0 0 ,Join,\",\",DLTB_Clip,DisNO,-1,-1", "INTERSECT", "", "")

    arcpy.AddField_management(DLTB_Clip,"MJ", "DOUBLE", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")
    arcpy.CalculateField_management(DLTB_Clip, "MJ", "!shape.area!", "PYTHON_9.3")
    with arcpy.da.UpdateCursor(DLTB_Clip,["MJ","KCMJ","ZLDWDM","TBBH","KCXZDWMJ"]) as cursor:
        for row in cursor:
            # print(arcpy.AddFieldDelimiters(XZDW_clip_SpatialJoin,"DisNO")+" like \"*"+row[1]+"*\"" )
            # print(row[1])
            with arcpy.da.SearchCursor(XZDW_clip,["长度","KD","KCBL"], where_clause="([KCTBDWDM1] = \""+row[2]+"\" and [KCTBBH1] = \""+ row[3]+"\") or ([KCTBDWDM2] = \""+row[2]+"\" and [KCTBBH2] =\""+ row[3]+"\")" ) as xzdw_cursor:
            # with arcpy.da.SearchCursor(XZDW_clip_SpatialJoin,["长度","KD","KCBL"], where_clause=arcpy.AddFieldDelimiters(XZDW_clip_SpatialJoin,"DisNO")+" like \"*"+row[1]+"*\"" ) as xzdw_cursor:
                mj = 0
                for xzdw in xzdw_cursor:
                    print(xzdw[0])
                    print(xzdw[1])
                    print(xzdw[2])
                    mj+= (xzdw[0]*xzdw[1]*xzdw[2])
            row[1] = round(mj,2)
            row[4] = row[0] - row[1]
            cursor.updateRow(row)
else:
    print("线状地物图层缺少字段！")
